package com.lagou.rpc.pojo;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Objects;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ServerDelayed implements Delayed {

    //清理间隔,单位秒
    private long cleanInterval=5;

    private ServerPath serverPath;

    @Override
    public long getDelay(TimeUnit timeUnit) {
        long l = serverPath.getServer().getDate().getTime() + cleanInterval * 1000 - System.currentTimeMillis();
        return timeUnit.convert(l,TimeUnit.MICROSECONDS);
    }

    @Override
    public int compareTo(Delayed delayed) {
        return Long.compare(getDelay(TimeUnit.MICROSECONDS),delayed.getDelay(TimeUnit.MICROSECONDS));
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ServerDelayed that = (ServerDelayed) o;
        return Objects.equals(serverPath, that.getServerPath());
    }

    @Override
    public int hashCode() {
        return serverPath.hashCode();
    }
}
